<script setup lang="tsx">
import { NButton, NTag } from 'naive-ui';
import { h } from 'vue';
import { useAuth } from '@/hooks/business/auth';
import { useTable, useTableOperate } from '@/hooks/common/table';
import { $t } from '@/locales';
import { DICT_TYPE, getDictObj } from '@/utils/dict';
import SvgIcon from '@/components/custom/svg-icon.vue';
import { type LogLogin, fetchGetLogLoginList } from '@/service/api';
import { useAppStore } from '@/store/modules/app';
import LogLoginSearch from './modules/log-login-search.vue';
import LogLoginDetailModel from './modules/log-login-detail-modal.vue';

const appStore = useAppStore();
const { hasAuth } = useAuth();

const { columns, data, loading, mobilePagination, getDataByPage, getData, searchParams, resetSearchParams } = useTable({
  apiFn: fetchGetLogLoginList,
  showTotal: true,
  apiParams: {
    current: 1,
    size: 10,
    username: null,
    userIp: null,
    createTime: null
  },
  columns: () => [
    {
      type: 'selection'
    },
    {
      key: 'id',
      title: $t('page.manage.log.login.id'),
      align: 'center',
      width: 100
    },
    {
      key: 'traceId',
      title: $t('page.manage.log.login.traceId'),
      align: 'center',
      width: 500,
      ellipsis: {
        tooltip: true
      }
    },
    {
      key: 'logType',
      title: $t('page.manage.log.login.logType'),
      align: 'center',
      width: 150,
      render: row => {
        if (row.logType === null) {
          return null;
        }
        const dictObj = getDictObj(DICT_TYPE.SYSTEM_LOGIN_TYPE, row.logType);

        return <NTag type={dictObj?.colorType}>{dictObj?.label}</NTag>;
      }
    },
    {
      key: 'username',
      title: $t('page.manage.log.login.username'),
      align: 'center',
      width: 150,
      ellipsis: {
        tooltip: true
      }
    },
    {
      key: 'userIp',
      title: $t('page.manage.log.login.userIp'),
      align: 'center',
      width: 150
    },
    {
      key: 'loginLocation',
      title: $t('page.manage.log.login.loginLocation'),
      align: 'center',
      width: 200
    },
    {
      key: 'browser',
      title: $t('page.manage.log.login.browser'),
      align: 'center',
      width: 150,
      ellipsis: {
        tooltip: true
      }
    },
    {
      key: 'os',
      title: $t('page.manage.log.login.os'),
      align: 'center',
      width: 150,
      ellipsis: {
        tooltip: true
      }
    },
    {
      key: 'result',
      title: $t('page.manage.log.login.result'),
      align: 'center',
      width: 150,
      render: row => {
        if (row.result === null) {
          return null;
        }
        const dictObj = getDictObj(DICT_TYPE.SYSTEM_LOGIN_RESULT, row.result);

        return <NTag type={dictObj?.colorType}>{dictObj?.label}</NTag>;
      }
    },
    {
      key: 'createTime',
      title: $t('page.manage.log.login.createTime'),
      align: 'center',
      width: 120
    },
    {
      key: 'operate',
      title: $t('common.operate'),
      align: 'center',
      fixed: 'right',
      width: 100,
      render: row => (
        <div class="flex-center gap-8px">
          <div v-show={hasAuth('system:dict_type:update')}>
            <NButton type="primary" ghost size="small" onClick={() => openDetail(row)}>
              {{
                default: () => $t('common.detail'),
                icon: () => h(SvgIcon, { icon: 'ph:eye' })
              }}
            </NButton>
          </div>
        </div>
      )
    }
  ]
});

const { modelVisible, editingData, handleEdit } = useTableOperate(data, getData);

function openDetail(row: LogLogin) {
  handleEdit(row.id);
}
</script>

<template>
  <div class="flex-col-stretch gap-16px overflow-hidden lt-sm:overflow-auto">
    <LogLoginSearch
      v-if="hasAuth('system:log-login:query')"
      v-model:model="searchParams"
      @reset="resetSearchParams"
      @search="getDataByPage"
    />
    <NCard
      :title="$t('page.manage.log.login.title')"
      :bordered="false"
      size="small"
      class="sm:flex-1-hidden card-wrapper"
    >
      <NDataTable
        :columns="columns"
        :data="data"
        size="small"
        :flex-height="!appStore.isMobile"
        :scroll-x="2088"
        :loading="loading"
        :row-key="row => row.id"
        remote
        :pagination="mobilePagination"
        class="sm:h-full"
      />

      <LogLoginDetailModel v-model:visible="modelVisible" :row-data="editingData" />
    </NCard>
  </div>
</template>

<style scoped></style>
